﻿using System;
using System.IO;

namespace fe.pdf.convert.jobs.webapp.Helper
{
    public static class PdfConverter
    {
        private static object Missing = Type.Missing;

        public static void Convert()
        {
            Type wordType = Type.GetTypeFromProgID("KWps.Application");

            if (wordType == null)
            {
                return;
            }

            try
            {
                object obj = "";
                object obj2 = @"d:\tmp\pdf\123.docx";
                string target = @"d:\tmp\pdf\123.pdf";
                dynamic word = Activator.CreateInstance(wordType);
                dynamic val2 = word.Documents.Open(ref obj2, ref Missing, ref Missing, ref Missing, ref obj);

                val2.ExportAsFixedFormat(target, 17);

                val2.Close(ref target);
                if (File.Exists(target))
                {
                    Console.WriteLine("成功!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public static void Convert(object source, string target)
        {
            Type wordType = Type.GetTypeFromProgID("KWps.Application");

            if (wordType == null)
            {
                throw new Exception("未获取到wps的实例,请检查wps是否安装正确!");
            }

            try
            {
                object obj = "";

                dynamic word = Activator.CreateInstance(wordType);
                dynamic val2 = word.Documents.Open(ref source, ref Missing, ref Missing, ref Missing, ref obj);

                val2.ExportAsFixedFormat(target, 17);

                val2.Close(ref target);
                if (File.Exists(target))
                {
                    Console.WriteLine("成功!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}